package sprint2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Het begin van het spel.
 * Incl de methodes van het laden & saven.
 *
 * @author Vincent van Andel
 * @author Fabian Mengerink
 *
 *
 */

public class Apl {

	static Boot boot = null;

	public static void main(String[] args) throws FileNotFoundException,
			IOException, ClassNotFoundException {
		
		menu();
	}

	/**
	 * Deze maakt een menu aan, en zorgt ervoor dat je in het spel zelf gaat komen
	 * En hier via kan je ook laden
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	private static void menu() throws FileNotFoundException, IOException,
			ClassNotFoundException {
		int choice = 0;
		System.out.println("1. Nieuw spel");
		System.out.println("2. Laden");
		Scanner in = new Scanner(System.in);
		try {
			choice = in.nextInt();
		} catch (InputMismatchException e) {
			System.out.println("Dit is geen geldige invoer ");
			in = new Scanner(System.in);
		}

		if (choice == 1) {
			newGame();
		}
		if (choice == 2) {
			laden();
		} else {
			System.out.println(" Geen geldige keuze");
			menu();
		}
	}

	/**
	 * Deze methode zorgt ervoor dat de Boot wordt geladen die op de schijf staat.
	 * Dit bestand bestaat altijd (zie save-methode).
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static void laden() throws FileNotFoundException, IOException,
			ClassNotFoundException {
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(
				"boot.dat"));
		boot = (Boot) in.readObject();
		boot.load();
	}

	public static void saven() throws FileNotFoundException, IOException {
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
				"boot.dat"));
		out.writeObject(boot);
	
	}

	private static void newGame() throws FileNotFoundException, IOException, ClassNotFoundException {
		boot = new Boot();
		boot.run();
	}
}
